<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/' }">商品</el-breadcrumb-item>
      <el-breadcrumb-item><a href="/">添加新品牌</a></el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图  -->
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>添加新品牌</span>
        <el-button @click="$router.go(-1)" type="info" style="float:right">返回</el-button>
      </div>
      <div class="box-item">
        <!-- 添加品牌表单  -->
        <el-form :model="addBrandForm" :rules="addBrandRules" ref="addBrandRef" :label-width="formLabelWidth" class="demo-ruleForm">
          <el-form-item label="品牌名称：" prop="name">
            <el-input v-model="addBrandForm.name"></el-input>
          </el-form-item>

          <el-form-item label="品牌首字母：">
            <el-input v-model="addBrandForm.letter"></el-input>
          </el-form-item>

          <el-form-item label="品牌LOGO：" prop="image">
            <el-upload
              class="upload-demo"
              :action="BASE_API+'/goods/youthOrange/uploadOss/upload/?host=imgsLibrary'"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              :on-success="handleAvatarSuccess"
               multiple
              :limit="3"
              :on-exceed="handleExceed"
              :file-list="fileList">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>

export default {
  data () {
    return {
      BASE_API: 'http://localhost:88/api',
      fileList: [{}],
      formLabelWidth: '170px',
      // 添加品牌表单
      addBrandForm: {},
      // 添加品牌规则
      addBrandRules: {
        name: [{ required: true, messages: '请输入品牌名称', trigger: 'blur' }],
        image: [{ required: true, messages: '请选择品牌LOG', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 上传成功的函数
    handleAvatarSuccess (res, file) {
        // response是上传阿里云oss成功后的结果，调用
        // console.log(res,URL.createObjectURL(file.raw)) // 上传响应，base64响应
        this.addBrandForm.image =  res.data.imgUrl
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },
    async onSubmit () {
      console.log(this.addBrandForm);
      // const { data: res } = await this.$http.get(`/goods/brand/add`)
      // if( res.code !== 200 ){
      //   this.$message.error('添加失败')
      // }
      // this.$message.success('添加成功')
      // this.$router.push('/goods/brand')
    }
  },
  components: {
  }
}
</script>
